package com.ddone.code.executor.controller;

import com.ddone.code.executor.pojo.entity.ColumnMetaData;
import com.ddone.code.executor.pojo.entity.DataSourceConfig;
import com.ddone.code.executor.pojo.entity.ExecuteResult;
import com.ddone.code.executor.pojo.enums.ExecutorType;
import com.ddone.code.executor.pojo.vo.ExecutorParamsVo;
import com.ddone.code.executor.service.ExecutorClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ddone
 * @date 2024/8/2-10:03
 */
@RestController
@RequestMapping("/code-executor")
@CrossOrigin
public class CodeExecutorController {

    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

    @Autowired
    private ExecutorClient executorClient;

    @PostMapping("/execute")
    public ExecuteResult execute(@RequestBody ExecutorParamsVo params) {
        String code = params.getCode();
        ExecutorType executeType = params.getExecuteType();
       return executorClient.executeTask(executeType,code);
    }

    @GetMapping("/schema")
    public List<String> getSchemaCompleter() {
        return executorClient.getExecutorSchemaCompletor(new DataSourceConfig());
    }

    @GetMapping("/table")
    public List<String> getTableCompleter(@RequestParam(value = "schema") String schema) {
        return executorClient.getExecutorTableCompleter(new DataSourceConfig(),schema);
    }

    @GetMapping("/column")
    public List<ColumnMetaData> getColumnCompleter(@RequestParam(value = "schema") String schema,
                                                   @RequestParam(value = "table") String table) {
        return executorClient.getExecutorColumnCompleter(new DataSourceConfig(),schema,table);
    }
}
